<?php
/**
 * (commands) Amazon订单
 */
namespace App\Console\Commands\SpApi\Order;

use App\Logic\Amazon\Api\Orders;
use App\Logic\Amazon\Api\RequiredConfig;
use App\Models\Amazon\AmazonOrderConditionBak;
use App\Models\Basics\AmazonAccount;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class GetOrdersByAccountIdBak extends Command
{
    /**
     * The name and signature of the console command.
     * 补单用
     * @var string
     */
    protected $signature = 'sp-order:get-orders-account-bak';

    /**
     * The console command description.
     * @var string
     */
    protected $description = 'exe func';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     */
    public function handle(): void
    {
        while (true){
            $endDate = "2023-12-18 01:00:00";
            $accountOrderCondition = AmazonOrderConditionBak::query()
//                ->where('account_id',31)
//                ->whereIn('account_id',[2,5,7,16,31,21,22,24,28])
//                ->whereIn('account_id',[32,35,39,41,45,47,48])
                ->where('next_start_time','<',$endDate)
                ->first();
            if(!$accountOrderCondition)break;
            $account = AmazonAccount::query()->find($accountOrderCondition->account_id);
            $requiredConfig = RequiredConfig::getInit();
            try {
                $config = $requiredConfig->configuration($account->refresh_token, $account->aws_region);
                $times = 6;//1个小时
                echo '---start---'.$account->account.'===do======='.PHP_EOL;
//                Log::log('info','---start---'.$account->account.'===do=======');
                Orders::apiGetOrdersBak($config,$account,null,null,$times);
                sleep(20);
                $next_start_time = AmazonOrderConditionBak::query()
                    ->where('account_id',$account->id)
                    ->value('next_start_time');
                if(strtotime($next_start_time) >= strtotime($endDate)){
//                    $account->api = 1;
//                    $account->save();
                    echo '---end---'.$account->account.'===all done======='.PHP_EOL;
//                    Log::log('info','---end---'.$account->account.'===all done=======');
                }else{
                    echo '---end---'.$account->account.'===done==='.PHP_EOL;
//                Log::log('info','---end---'.$account->account.'===done===');
                }
            } catch (\Exception $e) {
                var_dump('---accountId:---'.$account->account.'===error msg==='.$e->getTraceAsString());
//                Log::log('error','---accountId:---'.$account->account.'===error msg==='.$e->getTraceAsString());
            }
        }
        $this->info('finish!');
    }
}
